{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Buy Two Chocolates"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: buyChoco"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #购买两块巧克力"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组&nbsp;<code>prices</code>&nbsp;，它表示一个商店里若干巧克力的价格。同时给你一个整数&nbsp;<code>money</code>&nbsp;，表示你一开始拥有的钱数。</p>\n",
    "\n",
    "<p>你必须购买 <strong>恰好&nbsp;</strong>两块巧克力，而且剩余的钱数必须是 <strong>非负数</strong>&nbsp;。同时你想最小化购买两块巧克力的总花费。</p>\n",
    "\n",
    "<p>请你返回在购买两块巧克力后，最多能剩下多少钱。如果购买任意两块巧克力都超过了你拥有的钱，请你返回 <code>money</code>&nbsp;。注意剩余钱数必须是非负数。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>prices = [1,2,2], money = 3\n",
    "<b>输出：</b>0\n",
    "<b>解释：</b>分别购买价格为 1 和 2 的巧克力。你剩下 3 - 3 = 0 块钱。所以我们返回 0 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>prices = [3,2,3], money = 3\n",
    "<b>输出：</b>3\n",
    "<b>解释：</b>购买任意 2 块巧克力都会超过你拥有的钱数，所以我们返回 3 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= prices.length &lt;= 50</code></li>\n",
    "\t<li><code>1 &lt;= prices[i] &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= money &lt;= 100</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [buy-two-chocolates](https://leetcode.cn/problems/buy-two-chocolates/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [buy-two-chocolates](https://leetcode.cn/problems/buy-two-chocolates/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,2]\\n3', '[3,2,3]\\n3']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        if prices[0]+prices[1]>money:\n",
    "            return money\n",
    "        return money-prices[0]-prices[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        sortedPriceList = sorted(prices)\n",
    "        minCost =sortedPriceList[0] +sortedPriceList[1]\n",
    "        if minCost > money:\n",
    "            return money\n",
    "        else:\n",
    "            return money - minCost\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        ans = money - prices[0] - prices[1]\n",
    "        return ans if ans >=0 else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        a = b = inf\n",
    "        for x in prices:\n",
    "            if x < a:\n",
    "                b, a = a,x\n",
    "            elif x < b:\n",
    "                b = x\n",
    "        return money - a - b if a + b <= money else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        ans=-1\n",
    "        n=len(prices)\n",
    "        for i in range(n-1):\n",
    "            for j in range(i+1,n):\n",
    "                balance=money-prices[i]-prices[j]\n",
    "                if balance>ans:\n",
    "                    ans=balance\n",
    "        return ans if ans>=0 else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        if prices[0]+prices[1]>money:\n",
    "            return money\n",
    "        else:\n",
    "            return money-prices[0]-prices[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "\n",
    "        n = len(prices)\n",
    "        min_ = min(prices)\n",
    "        less = [0] * n\n",
    "        less_count = 0\n",
    "        for i in range(0,n):\n",
    "            if prices[i] != min_:\n",
    "                less[i] = min_\n",
    "            else:\n",
    "                less_count += 1\n",
    "        \n",
    "        ans = -1\n",
    "        for i in range(0,n):\n",
    "            if less[i] != 0:\n",
    "                ans = max(ans, money - prices[i] - less[i])\n",
    "        \n",
    "        if less_count >= 2:\n",
    "            ans = max(ans, money - min_ * 2)\n",
    "        if ans == -1:\n",
    "            return money\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        if prices[0] + prices[1] <= money:\n",
    "            return money - prices[0] - prices[1]\n",
    "        else:\n",
    "            return money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices = sorted(prices)\n",
    "        min_p = prices[0]+prices[1]\n",
    "        if(min_p<=money):\n",
    "            return money-min_p\n",
    "        else:\n",
    "            return money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        a = b = inf\n",
    "        for x in prices:\n",
    "            if x < a:\n",
    "                    b, a = a, x\n",
    "            elif x < b:\n",
    "                    b = x\n",
    "        return money - a - b if a + b <= money else money\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        a=b=inf\n",
    "        for x in prices:\n",
    "            if x<a:\n",
    "                b,a=a,x\n",
    "            elif x<b:\n",
    "                b=x\n",
    "        return money-a-b if a+b<=money else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        min1 = min2 = float('inf')\n",
    "        for price in prices:\n",
    "            if price < min1:\n",
    "                min2 = min1\n",
    "                min1 = price\n",
    "            elif price < min2:\n",
    "                min2 = price\n",
    "        if money < min1 + min2:\n",
    "            return money\n",
    "        else:\n",
    "            return money - min1 - min2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        \n",
    "        prices.sort()\n",
    "        ans=money-prices[0]-prices[1]\n",
    "        return ans if ans>=0 else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        xiao1 = min(prices[0:2])\n",
    "        xiao2 = max(prices[0:2])\n",
    "        for val in prices[2:] :\n",
    "            if val < xiao1 :\n",
    "                xiao2, xiao1 = xiao1, val\n",
    "            elif val < xiao2 :\n",
    "                xiao2 = val\n",
    "        return money - xiao1 - xiao2 if xiao1 + xiao2 <= money else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        if prices[0] + prices[1] > money:\n",
    "            return money\n",
    "        else:\n",
    "            return money - prices[0] - prices[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        return money - (prices[0] + prices[1]) if money - (prices[0] + prices[1]) >= 0 else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        return money if sum(prices[:2]) > money else money - sum(prices[:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices = sorted(prices)\n",
    "        return money if (prices[0]+prices[1]) > money else money-(prices[0]+prices[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        x = y = inf\n",
    "        for p in prices:\n",
    "            if p < x:\n",
    "                y = x\n",
    "                x = p\n",
    "            elif p < y:\n",
    "                y = p\n",
    "        price = x+y\n",
    "        ans = money - price\n",
    "        return ans if ans >= 0 else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        return money if sum(prices[:2])>money else money-sum(prices[:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        sums=prices[0]+prices[1]\n",
    "        if sums>money:\n",
    "            return money\n",
    "        else:\n",
    "            return money-sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        if money < prices[0] + prices[1]:\n",
    "            return money\n",
    "        else:\n",
    "            return money - prices[0] - prices[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        if sum(prices[0:2])>money:\n",
    "            return money\n",
    "        else:\n",
    "            return money-sum(prices[0:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        min1 = min2 = inf\n",
    "        for p in prices:\n",
    "            if p < min1:\n",
    "                min2 = min1\n",
    "                min1 = p\n",
    "            elif p < min2:\n",
    "                min2 = p\n",
    "        minsum = min1 + min2\n",
    "        return money-minsum if minsum <= money else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        sums=prices[0]+prices[1]\n",
    "        if sums>money:\n",
    "            return money\n",
    "        else:\n",
    "            return money-sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        Min=min(prices)\n",
    "        sums=Min\n",
    "        prices.remove(Min)\n",
    "        sums+=min(prices)\n",
    "        if sums>money:\n",
    "            return money\n",
    "        else:\n",
    "            return money-sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: list[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        if prices[0] + prices[1] <= money:\n",
    "            return money - prices[0] -prices[1]\n",
    "        else:\n",
    "            return money\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        Min=min(prices)\n",
    "        sums=Min\n",
    "        prices.remove(Min)\n",
    "        sums+=min(prices)\n",
    "        if sums>money:\n",
    "            return money\n",
    "        else:\n",
    "            return money-sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        a = b = inf\n",
    "        for x in prices:\n",
    "            if x < a:\n",
    "                b, a = a, x\n",
    "            elif x < b:\n",
    "                b = x\n",
    "        return money - a - b if a + b <= money else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        mn1 = mn2 = inf\n",
    "        for p in prices:\n",
    "            if p < mn1:\n",
    "                mn2 = mn1\n",
    "                mn1 = p\n",
    "            elif p < mn2:\n",
    "                mn2 = p\n",
    "        return money if mn1 + mn2 > money else money - mn1 - mn2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        sums=prices[0]+prices[1]\n",
    "        if sums>money:\n",
    "            return money\n",
    "        else:\n",
    "            return money-sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        left = money - prices[0] - prices[1]\n",
    "        return left if left >= 0 else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        min_cost = sum(prices[:2])\n",
    "        return money-min_cost if min_cost <= money else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        res = sorted(prices)[0] + sorted(prices)[1]\n",
    "        return money-res if (money-res) >= 0 else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        res = money-prices[0]-prices[1]\n",
    "        if res>=0:\n",
    "            return res\n",
    "        else:\n",
    "            return money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        mn1 = float('inf')\n",
    "        mn2 = float('inf')\n",
    "        for p in prices:\n",
    "            if p<=mn1:\n",
    "                mn2 = mn1\n",
    "                mn1 = p\n",
    "            elif p<=mn2:\n",
    "                mn2 = p\n",
    "        \n",
    "        if money>=mn1+mn2:\n",
    "            return money-(mn1+mn2)\n",
    "        return money\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        if prices[0]+prices[1] <= money:\n",
    "            return money - prices[0] - prices[1]\n",
    "        else:\n",
    "            return money\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        min = money + 1\n",
    "        left = 0\n",
    "        right = len(prices) - 1\n",
    "        while left < right:\n",
    "            # 金融判断\n",
    "            if min > prices[left] + prices[right]:\n",
    "                min = prices[left] + prices[right]\n",
    "            # 往中间推进\n",
    "            if prices[left] > prices[right]:\n",
    "                left += 1\n",
    "            else:\n",
    "                right -= 1\n",
    "        if min > money:\n",
    "            return money\n",
    "        return money - min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        k = money - prices[0] - prices[1]\n",
    "        return  k if k >= 0 else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "\n",
    "        min_v,sec_v = inf,inf\n",
    "\n",
    "        for x in prices:\n",
    "            if x <min_v:\n",
    "                sec_v =min_v\n",
    "                min_v = x\n",
    "            elif x < sec_v:\n",
    "                sec_v =x\n",
    "\n",
    "        return  money if min_v +sec_v > money else money -min_v-sec_v \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        mn1 = mn2 = inf\n",
    "        for p in prices:\n",
    "            if p < mn1:\n",
    "                mn2 = mn1\n",
    "                mn1 = p\n",
    "            elif p < mn2:\n",
    "                mn2 = p\n",
    "        return money if mn1 + mn2 > money else money - mn1 - mn2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        if prices[0]+prices[1] <= money:\n",
    "            return money-prices[0]-prices[1]\n",
    "        else:\n",
    "            return money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        if prices[0] + prices[1] > money:\n",
    "            return money\n",
    "        return money - (prices[0] + prices[1])\n",
    "\n",
    "\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        n = money - (prices[0] + prices[1])\n",
    "        return money if n < 0 else n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        if sum(prices[0:2])>money:\n",
    "            return money\n",
    "        else:\n",
    "            return money-sum(prices[0:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        if prices[0] + prices[1] > money:\n",
    "            return money\n",
    "        else:\n",
    "            return money - prices[0] - prices[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        return money-prices[0]-prices[1] if money>=sum(prices[:2]) else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "\n",
    "        n = len(prices)\n",
    "        ans = -1\n",
    "        for i in range(0,n):\n",
    "            for j in range(i+1,n):\n",
    "                if prices[i] + prices[j] > money:\n",
    "                    continue;\n",
    "                else:\n",
    "                    ans = max(ans, money - prices[i] - prices[j])\n",
    "        if ans < 0:\n",
    "            return money\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        # 返回两数和的最小值 超过了money 返回money\n",
    "        # \n",
    "        prices.sort()\n",
    "        left = money- sum(prices[0:2])\n",
    "        if left >=0 :\n",
    "            return left\n",
    "        else:\n",
    "\n",
    "            return money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        sums=prices[0]+prices[1]\n",
    "        if sums>money:\n",
    "            return money\n",
    "        else:\n",
    "            return money-sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        xiao1 = xiao2 = inf\n",
    "        for val in prices :\n",
    "            if val < xiao1 :\n",
    "                xiao2, xiao1 = xiao1, val\n",
    "            elif val < xiao2 :\n",
    "                xiao2 = val\n",
    "        return money - xiao1 - xiao2 if xiao1 + xiao2 <= money else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "      n=len(prices)\n",
    "      prices.sort()\n",
    "      a=prices[0]+prices[1]\n",
    "      if a>money:\n",
    "          return money\n",
    "      if a==money:\n",
    "          return 0\n",
    "      if a<money:\n",
    "          return money-a\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        sums=prices[0]+prices[1]\n",
    "        if sums>money:\n",
    "            return money\n",
    "        else:\n",
    "            return money-sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        mn1 = mn2 = inf\n",
    "        for p in prices:\n",
    "          if p < mn1:\n",
    "              mn2 = mn1\n",
    "              mn1 = p\n",
    "          elif p < mn2:\n",
    "              mn2 = p\n",
    "        return money if mn1 + mn2 > money else money - mn1 -mn2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        a=[prices[0],prices[1]]\n",
    "        a.sort()\n",
    "        for i in prices[2:]:\n",
    "            if i<a[1]:\n",
    "                a[1]=i\n",
    "            if a[0]>a[1]:\n",
    "                a[0],a[1]=a[1],a[0]\n",
    "        a=sum(a)\n",
    "        if a>money:\n",
    "            return money\n",
    "        else:\n",
    "            return money-a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        Min=min(prices)\n",
    "        sums=Min\n",
    "        prices.remove(Min)\n",
    "        sums+=min(prices)\n",
    "        if sums>money:\n",
    "            return money\n",
    "        else:\n",
    "            return money-sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        minPrice = sorted(prices)[0] +sorted(prices)[1]\n",
    "        if minPrice > money:\n",
    "            return money\n",
    "        else:\n",
    "            return money - minPrice\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        a, b, n = prices[0], prices[1], len(prices)\n",
    "        if a > b:\n",
    "            a, b = b, a\n",
    "        for i in range(2, n):\n",
    "            if prices[i] < a:\n",
    "                a, b = prices[i], a\n",
    "            elif a <= prices[i] < b:\n",
    "                b = prices[i]\n",
    "        if a + b > money:\n",
    "            return money\n",
    "        else:\n",
    "            return money - a - b\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        return money - sum(prices[:2]) if money >= sum(prices[:2]) else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        price = sum(prices[0:2])\n",
    "        return (money-price) if money >= price else money\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        Min=min(prices)\n",
    "        sums=Min\n",
    "        prices.remove(Min)\n",
    "        sums+=min(prices)\n",
    "        if sums>money:\n",
    "            return money\n",
    "        else:\n",
    "            return money-sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        mn1 = mn2 = inf\n",
    "        for p in prices:\n",
    "            if p < mn1:\n",
    "                mn2 = mn1\n",
    "                mn1 = p\n",
    "            elif p < mn2:\n",
    "                mn2 = p\n",
    "        return money if mn1 + mn2 > money else money - mn1 - mn2\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        minPrice = prices[0] + prices[1]\n",
    "        result = money if minPrice > money else money - minPrice\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        sums=prices[0]+prices[1]\n",
    "        if sums>money:\n",
    "            return money\n",
    "        else:\n",
    "            return money-sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices = sorted(prices)\n",
    "        n = money - prices[0] - prices[1]\n",
    "        if n >= 0:\n",
    "            return n\n",
    "        else:\n",
    "            return money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        return money if (tmp := money - sum(sorted(prices)[:2])) < 0 else tmp   \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        small = prices[0]\n",
    "        big = prices[1]\n",
    "        if big < small:\n",
    "            big, small = small, big\n",
    "        for i in range(2, len(prices)):\n",
    "            if prices[i] <= small:\n",
    "                big = small\n",
    "                small = prices[i]\n",
    "            elif prices[i] > small and prices[i] < big:\n",
    "                big = prices[i]\n",
    "        total = big + small\n",
    "        return money - total if money - total >= 0 else money"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def buyChoco(self, prices: List[int], money: int) -> int:\n",
    "        prices.sort()\n",
    "        new_money = money\n",
    "        money -= sum(prices[:2])\n",
    "        if money < 0:\n",
    "            return new_money\n",
    "\n",
    "        else:\n",
    "            return money"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
